home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9749 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.telepac.pt!usenet
  2. From: vortex@telepac.pt (VORTEX)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: confusion between putchar and printf
  5. Date: Wed, 13 Mar 1996 10:14:18 GMT
  6. Organization: telepac
  7. Message-ID: <4i5lfv$ltv@vivaldi.telepac.pt>
  8. References: <4i1v2n$30o@news.azstarnet.com> <4i28j7INN65d@keats.ugrad.cs.ubc.ca>
  9. Reply-To: vortex@telepac.pt
  10. NNTP-Posting-Host: por1_p7.telepac.pt
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
  14.  
  15. >In article <4i1v2n$30o@news.azstarnet.com>,
  16. >Howard Salmon  <captarm@azstarnet.com> wrote:
  17. >>K & R (section 1.5) states that "putchar(c) prints the contents of the 
  18. >>integer variable c as a character, usually on the screen".
  19. > ^^^^^^^^^^^^^^^^        ^^^^^^^^^
  20.  
  21. >>Here's a small program that I wrote testing putchar.  Why doesn't it 
  22. >>compile?
  23. >>
  24. >>#include <stdio.h>
  25. >># define A "hello world!"
  26. >>
  27. >>main()
  28. >>{
  29. >>    int c;
  30. >>    c =  A;
  31. >>    putchar(A);
  32. >>}
  33.  
  34. >Since when can you assign a string literal constant such as "hello world!" to
  35. >an _integer_ variable? 
  36.  
  37. >The C language has a concept of type. The values you assign to a variable
  38. >must have a type that is compatible with the variable's type. 
  39.  
  40. >Exactly what are you expecting this program to do?  A single call to putchar is
  41. >incapable of emitting a string no matter what argument you give to it. It
  42. >prints a single character, exactly as K&R says. You can never get a single call
  43. >to putchar to place a string on the screen. Try this:
  44.  
  45. >#include <stdio.h>
  46.  
  47. >int main()
  48. >{
  49. >    char *p = "hello, world!\n";
  50.  
  51. >    /* The string literal "hello, world\n" is stored in static memory.
  52. >       The variable p is a character pointer which initially holds the
  53. >       address of the character 'h'. */
  54.  
  55. >    while (*p)    /* '*p' refers to the character pointed at by p    */
  56. >            /* we loop while this is not the zero character    */
  57. >        putchar(*p++);    /* print that character,  & increment p    */
  58. >                /* to point to the next character    */
  59.  
  60. >    return 0;    /* all C programs need to return an exit status    */
  61. >}
  62. >-- 
  63.  
  64.  
  65.  
  66.  
  67. No they don'r
  68.  
  69. Just star with  void main(void) and you need to return any value
  70.  
  71.